<?php 
namespace XFCore\controllers\api;
use lib\{BaseController , Examine};
use lib\protocol\{AliQQ_PayMoney,QQPC};
class CronController extends BaseController{
    public function __construct() {
        parent::__construct();
        
    }
    //一秒一次
    public function sxorder($get){
        $this->cronkeyauth($get['key']);
        $data = [
            'status'=>0,
            'outtime'=>[
                'operator'=>'lt',
                'value'=>time()
            ],
        ];
        $options = [
            'orderBy'=>'rand()',
            'limit'=>cronlimit
        ];
        $rs = $this->db->select('order','`id`,`qr_id`',$data,$options);
        $srow = '';
        //处理更新订单状态
        if($rs['count']>0){
            foreach ($rs['result'] as $res){
                $this->db->update("order",['status'=>2],['id'=>$res['id']]);
            }
            $srow = $rs['result'];
        }
       $this->echomsg("html","Cron ok!");
    }
    //5秒执行一次检查
    public function wxyun($get){
        $this->cronkeyauth($get['key']);
        $data = [
            'status'=>1,
            'hook_type'=>2,
            'type'=>'wxpay',
            'crontime'=>[
                'operator'=>'lt',
                'value'=>time() 
            ],
        ];
        $options = [
            'limit'=>cronlimit
        ];
        $rs = $this->db->select('qrlist','`id`,`pid`,`xf_type`,`wxyunurl`,`cookie`',$data,$options);
        if($rs['count'] > 0){
            foreach ($rs['result'] as $res){
                $this->wxyunuptime($res);
            }
        }
        $this->echomsg("html","Cron ok!");
    }
    
    //1秒
    public function MapiNotify($get){
        $this->cronkeyauth($get['key']);
        $data = [
            'status'=>0,
            'pay_sign'=>"mapi",
            'outtime'=>[
                'operator'=>'lt',
                'value'=>time() 
            ],
        ];
        $options = [
            'limit'=>cronlimit
        ];
        $rs = $this->db->select('order','*',$data,$options);
        
        if($rs['count']>0){
            foreach ($rs['result'] as $row){
                $qrdata = [
                    'id'=>979,
                    'cookie'=>[
                        'operator'=>'notin',
                        'value'=>['xfqydmfxy']
                    ],
                    'hook_type'=>[
                        'operator'=>'notin',
                        'value'=>['1','3']
                    ]
                ];
                $options = [
                    'single_row'=>true,
                    'limit'=>1
                ];
                $qr_row = $this->db->select('qrlist',"*",$qrdata,$options)['result'];
                if($qr_row){
                    $this->MapiNotifyDeal($qr_row,$row);
                }
            }
        }
        $this->echomsg("html","Cron ok!");
    }
    //15秒
    public function wx_dytime($get){
        $this->cronkeyauth($get['key']);
        $data = [
            'status'=>1,
            'login_time'=>[
                'operator'=>'lt',
                'value'=>time()
            ]
        ];
        $options = [
            'limit'=>cronlimit
        ];
        $rs = $this->db->select('wechat_trumpet','id',$data,$options);
        if($rs['count']>0){
            foreach ($rs['result'] as $res){
                $this->db->update('wechat_trumpet',['login_time'=>time() + 60] , ['id'=>$res['id']]);
            }
        }
        $this->echomsg("html","Cron ok!");
    }
    //3秒
    public function aliqqyun($get){
        $this->cronkeyauth($get['key']);
        $data = [
            'status'=>1,
            'crontime'=>[
                'operator'=>'lt',
                'value'=>time()
            ],
            'type'=>[
                'operator'=>'in',
                'value'=>['alipay','qqpay']
            ],
            'cookie'=>[
                'operator'=>'notin',
                'value'=>['xfqymckxy','xfqydmfxy']
            ],
            'hook_type'=>0
        ];
        $options = [
            'limit'=>cronlimit
        ];
        $rs = $this->db->select('qrlist','`id`,`pid`,`type`,`cookie`,`xf_type`,`xf_stat`,`beizhu`',$data,$options);
        if($rs['count'] > 0){
            foreach ($rs['result'] as $res){
                // var_dump($res);
                
                $this->ALIQQ_Cron_Money($res);
            }
        }
        $this->echomsg("html","Cron ok!");
    }
    
    //1分钟
    public function fkjk($get){
        $this->cronkeyauth($get['key']);
        $rs = $this->db->select('qrlist',"id,status,pid",['stat'=>1],['limit'=>cronlimit]);
        
        if($rs['count'] > 0){
            foreach ($rs['result'] as $res){
                $count = $this->db->select('order',"count(*) as count" , ['qr_id'=>$res['id'] , 'status'=>2 , 'outtime'=>['operator'=>'gt' ,'value'=>time()-3600*12]] , ['single_row'=>true])['result']['count'];
                if($count > 20){
                    if($res['status'] == 1){
                        $userrow = $this->db->userrow($res['pid'] , 'pid','email,user');
                        $userrow['id'] = $res['id'];
                        $this->db->update('qrlist',['status'=>0,'stat'=>0],['id'=>$res['id']]);
                        if($userrow['email']){
                            $msg = $this->MsgNotice->send('fkjk',$userrow,"风控检测");
                            $sub = '风控监控 -' . $this->conf->sitename;
                            send_mail($email, $sub, $msg);
                        }
                    }
                }
            }
        }
        $this->echomsg("html","Cron ok!");
    }
    //1分钟
    public function qlwzf($get){
        $this->cronkeyauth($get['key']);
        $count = $this->db->select('order',"count(*) as count",['status'=>2,'outtime'=>['operator'=>'lt','value'=>time()-3600*24]],['single_row'=>true])['result']['count'];
        if($count >0){
            $this->db->complexdelete('order',['status'=>2,'outtime'=>['<',time()-3600*24]]);
        }
        $this->echomsg("html","Cron ok!");
    }
    public function user_vips($get){
        $this->cronkeyauth($get['key']);
        $rs = $this->db->select('user','`pid`,`alipay_free_vip_time`,`qqpay_free_vip_time`,`wxpay_free_vip_time`,`email`,`user`',['vip_mail'=>1],['limit'=>cronlimit]);
        $time = time();
        
        if($rs['count']>0){
            foreach ($rs['result'] as $res){
                
                if(strtotime($res['alipay_free_vip_time'])<$time || strtotime($res['wxpay_free_vip_time'])<$time || strtotime($res['qqpay_free_vip_time'])<$time){
                    $this->userr_viptime($res);
                }
            }
        }
        $this->echomsg("html","Cron ok!");
    }
    //5秒
    public function appcron($get){
        $this->cronkeyauth($get['key']);
        $data = [
            'status'=>1,
            'crontime'=>[
                'operator'=>'lt',
                'value'=>time()
            ],
            'type'=>[
                'operator'=>'in',
                'value'=>['alipay','wxpay']
            ],
            'xf_type'=>3,
            'hook_type'=>1
        ];
        $options = [
            'limit'=>cronlimit
        ];
        $rs = $this->db->select('qrlist',"`id`,`pid`,`type`",$data,$options);
        if($rs['count'] > 0){
            foreach ($rs['result'] as $res){
                $this->app_dx_notify($res);
            }
        }
        $this->echomsg("html","Cron ok!");
    }
    protected function app_dx_notify($qr_row){
        $email = $this->db->userrow($qr_row['pid'],'pid','email')['email'];
        $E_type = array(
            'wxpay'=>'微信APP挂机',
            'alipay'=>'支付宝APP挂机'
        );
        if($email){
            $msg = $this->MsgNotice->send('Drop_Notify',$email,$E_type[$qr_row['type']]);
           
            $sub = '掉线通知 -' . $this->conf->sitename;
            send_mail($email, $sub, $msg);
        }
        $this->db->update('qrlist',['status'=>0,'cookie'=>0,'endtime'=>$this->date],['id'=>$qr_row['id']]);
    }
    protected function userr_viptime($userrow){
        
       if($userrow['email']){
           
           $msg = $this->MsgNotice->send('user_vip_email',$userrow,'会员过期通知');
           
           $sub = '会员过期提醒 -' . $this->conf->sitename;
           send_mail($userrow['email'], $sub, $msg);
           
       }
       $this->db->update('user',['vip_mail'=>0],['pid'=>$userrow['pid']]);
    }
    
    protected function ALIQQ_Cron_Money($row){
        
        $timess=time()+  mt_rand(30, 45);
        if ($row['type'] == 'alipay'){
            $methodName = ($row['xf_type'] == '1') ? 'Get_pay_money' : 'Get_pay_money' . $row['xf_type'];
            $Pay_Money_Api = AliQQ_PayMoney::$methodName($row['type'], $row['cookie']);
            
        }else {
            $Pay_Money_Api = AliQQ_PayMoney::Get_pay_money($row['type'], $row['cookie']);
        }
        $E_type = array(
            'qqpay'=>'QQ云端',
            'alipay'=>'支付宝云端'
        );
        $dx = false;
        if($Pay_Money_Api['status'] === false){
            if($row['type'] == 'qqpay' && $row['xf_stat'] == 1){
                $qqpc = new QQPC();
                $res  = $qqpc->GET_JIANCE('qqpay',$row['beizhu']);
                if($res['code'] == 0 && !empty($res['cookie'])){
                    $this->db->update('qrlist',['cookie'=>$res['cookie'] ,'crontime'=>$timess],['id'=>$row['id']]);
                    return false;
                }else{
                    $dx = true;
                }
            }else{
                $dx = true;
            }
        }else{
            $money = $Pay_Money_Api['money']>=0.01 ? $Pay_Money_Api['money'] : 0.00;
            $this->db->update('qrlist',['crontime'=>$timess,'money'=>$money],['id'=>$row['id']]);
            return false;
        }
        if($dx){
            $email = $this->db->userrow($row['pid'] , 'pid' , 'email')['email'];
            if($email){
                $msg = $this->MsgNotice->send('Drop_Notify',$email,$E_type[$row['type']]);
                $sub = '掉线通知 -' . $this->conf->sitename;
                send_mail($email, $sub, $msg);
            }
            $this->db->update('qrlist',['cookie'=>0,'status'=>0,'endtime'=>$this->date],['id'=>$row['id']]);
            return true;
        }
    }
    protected function MapiNotifyDeal($qr_row , $srow){
        switch ($srow['type']){
            case 'alipay':
            case 'qqpay':
                if($qr_row['cookie'] == 'xfqymckxy' && $qr_row['type'] == 'alipay'){
                    xufeiqymoneymck($qr_row,$srow);
                }else{
                    ALIQQ_money_notify($qr_row, $srow);
                }
                break;
            case 'wxpay':
                WxYun_Notidfy($qr_row,$srow);
                break;
            case 'usdt':
                check_usdt($qr_row,$srow);
                break;
        }
    }
    protected function wxyunuptime($qr_row){
        $timess = time() +  mt_rand(45, 80);
        $WxApi = Examine::Wx_protocol($qr_row);
        $WXHeart = $WxApi->WXHeartBeat($qr_row['cookie']);
        if($WXHeart['code'] == 1){
            $this->db->update('qrlist',['crontime'=>$timess],['id'=>$qr_row['id']]);
        }else{
            $email = $this->db->userrow($qr_row['pid'] , 'pid' , 'email')['email'];
            if($email){
                $msg = $this->MsgNotice->send('Drop_Notify',$email,'微信云端');
                $sub = '掉线通知 -' . $this->conf->sitename;
                send_mail($email, $sub, $msg);
            }
            $this->db->update('qrlist',['status'=>0,'cookie'=>0,'endtime'=>$this->date],['id'=>$qr_row['id']]);
        }
    }
    
    protected function cronkeyauth($key){
        if($key != $this->conf['cronkey'])$this->echomsg("您的KEY有误请核对后在试!");
    }
}
?>